package edu.gatech.stolen.tea;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class ItemDisplayListActivity extends ListActivity {
	
	public static final String TAG = "Item Page";
	
	public static ArrayList<ToDoItem> yourItems;
	private Spinner filter;
	private Spinner[] dateSpinners = new Spinner[3];
	private Button createButton;
	private Button filterButton;
	private ItemDisplayListAdapter adapter;
	private String[] options;
	private Month[] months = new Month[12];
	private Day[] days = new Day[31];
	private Integer[] years = new Integer[50];
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.itempage);
        
      	yourItems = ItemManager.getUserItems();
        
      	adapter = new ItemDisplayListAdapter(this, R.layout.itemdisplaylistrow, yourItems);
      	setListAdapter(new ItemDisplayListAdapter(this, R.layout.itemdisplaylistrow, yourItems));
      	
      	options = new String[6];
      	options[0] = "All"; options[1] = "Personal"; options[2] = "Work"; options[3] = "School";
      	options[4] = "Done"; options[5] = "Date";
      	
        filter = (Spinner) findViewById(R.id.sFilterSpinner_itempage);
        ArrayAdapter<String> filterAdapter = new ArrayAdapter<String>(this, R.layout.filterrow, R.id.tFilterTextView_filter, options);
        filter.setAdapter(filterAdapter);
        filter.setSelection(0);
        
        populateData();
        
        dateSpinners[0] = (Spinner) findViewById(R.id.sMonthSpinner_itempage);
        ArrayAdapter<Month> mAdapter = new ArrayAdapter<Month>(this, R.layout.monthfilter, R.id.tMonthFilterTextView_monthfilter, months);
        dateSpinners[0].setAdapter(mAdapter);
        dateSpinners[0].setSelection(0);
        dateSpinners[0].setVisibility(1);
        
        dateSpinners[1] = (Spinner) findViewById(R.id.sDaySpinner_itempage);
        ArrayAdapter<Day> dAdapter = new ArrayAdapter<Day>(this, R.layout.dayfilter, R.id.tDayFilterTextView_dayfilter, days);
        dateSpinners[1].setAdapter(dAdapter);
        dateSpinners[1].setSelection(0);
        dateSpinners[1].setVisibility(1);
        
        dateSpinners[2] = (Spinner) findViewById(R.id.sYearSpinner_itempage);
        ArrayAdapter<Integer> yAdapter = new ArrayAdapter<Integer>(this, R.layout.yearfilter, R.id.tYearFilterTextView_yearfilter, years);
        dateSpinners[2].setAdapter(yAdapter);
        dateSpinners[2].setSelection(0);
        dateSpinners[2].setVisibility(1);
        
        createButton = (Button) findViewById(R.id.bCreateButton_itempage);
        filterButton = (Button) findViewById(R.id.bFilterButton_itempage);
        
		createButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				onCreateButtonClicked();
				
			}
		});
		
		filter.setOnItemSelectedListener((new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
				
				if(position == 5){
					dateSpinners[0].setVisibility(1);
					dateSpinners[1].setVisibility(1);
					dateSpinners[2].setVisibility(1);
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		}));
		
		filterButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				onFilterButtonClicked();
				
			}
		});
		
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	
    	Log.v(TAG, "Item Clicked");
    	super.onListItemClick(l, v, position, id);
    	
    	ToDoItem item = yourItems.get(position);
    	ItemManager.setCurrentItem(item);
    
    }
    
    protected void onResume() {
    	
    	super.onResume();
    	
    }
    
    private void onCreateButtonClicked() {
    	
    	Log.v(TAG, "Create Button Clicked");
    	goToCreateItemPage();
    	
    }
    
    private void onFilterButtonClicked() {
    	
    	Log.v(TAG, "Filter Button Clicked");
    	filter();
    	
    }
    
    private void filter() {
    	
    	int i = filter.getSelectedItemPosition();
    	
    	if(i == 0){
    		yourItems = ItemManager.getHolder().getAll();
    	}
    	else if(i == 1){
    		yourItems = ItemManager.getHolder().getType(ItemType.PERSONAL);
    	}
    	else if(i == 2){
    		yourItems = ItemManager.getHolder().getType(ItemType.WORK);
    	}
    	else if(i == 3){
    		yourItems = ItemManager.getHolder().getType(ItemType.SCHOOL);
    	}
    	else if(i == 4){
    		yourItems = ItemManager.getHolder().getDone();
    	}
    	else if(i == 5){
    		Month month = months[dateSpinners[0].getSelectedItemPosition()];
        	Day day = days[dateSpinners[1].getSelectedItemPosition()];
        	int year = years[dateSpinners[2].getSelectedItemPosition()];
    		month.setDay(day); month.setYear(year);
    		yourItems = Filter.byDate(month, yourItems);
    	}
    	
    	setListAdapter(new ItemDisplayListAdapter(this, R.layout.itemdisplaylistrow, yourItems));
    	adapter.notifyDataSetChanged();
    	
    }
    
    private void populate(){
    	
    	Month m = Month.APRIL;
		m.setDay(Day.EIGHTH);
		m.setYear(1999);
    	ItemManager.createItem("demo", "demo", "demo", m, m, ItemType.PERSONAL);
    	ItemManager.createItem("ademo", "demo", "demo", m, m, ItemType.SCHOOL);
    	ItemManager.createItem("aemo", "demo", "demo", m, m, ItemType.WORK);
    	ItemManager.createItem("vemo", "demo", "demo", m, m, ItemType.PERSONAL);
    	ItemManager.createItem("semo", "demo", "demo", m, m, ItemType.WORK);
    	
    	yourItems = ItemManager.getUserItems();
    	
    }
    
    private void populateData() {
    	
        months[0] = Month.JANUARY; months[1] = Month.FEBRUARY; months[2] = Month.MARCH; months[3] = Month.APRIL;
        months[4] = Month.MAY; months[5] = Month.JUNE; months[6] = Month.JULY; months[7] = Month.AUGUST;
        months[8] = Month.SEPTEMBER; months[9] = Month.OCTOBER; months[10] = Month.NOVEMBER; months[11] = Month.DECEMBER;
        
        days[0] = Day.FIRST; days[1] = Day.SECOND; days[2] = Day.THIRD; days[3] = Day.FOURTH; days[4] = Day.FIFTH;
        days[5] = Day.SIXTH; days[6] = Day.SEVENTH; days[7] = Day.EIGHTH; days[8] = Day.NINTH; days[9] = Day.TENTH;
        days[10] = Day.ELEVENTH; days[11] = Day.TWELVTH; days[12] = Day.THIRTHEENTH; days[13] = Day.FOURTEENTH; days[14] = Day.FIFTEENTH;
        days[15] = Day.SIXTENTH; days[16] = Day.SEVENTEENTH; days[17] = Day.EIGTEENTH; days[18] = Day.NINETEENTH; days[19] = Day.TWENTIETH;
        days[20] = Day.TWENTY_FIRST; days[21] = Day.TWENTY_SECOND; days[22] = Day.TWENTY_THIRD; days[23] = Day.TWENTY_FOURTH; days[24] = Day.TWENTY_FIFTH;
        days[25] = Day.TWENTY_SIXTH; days[26] = Day.TWENTY_SEVETH; days[27] = Day.TWENTY_EIGHTH; days[28] = Day.TWENTY_NINTH; days[29] = Day.THIRTIETH;
        days[30] = Day.THIRTY_FIRST;
        
        for(int i = 0; i < 50; i++){
        	years[i] = Integer.valueOf(i + 2012);
        }
    	
    }
    
    protected void goToCreateItemPage(){
    	
    	Intent i = new Intent(this, CreateItemActivity.class);
    	startActivity(i);
    	
    }
    
    protected void goToEditItemPage(){
    	
    	Intent i = new Intent(this, EditItemActivity.class);
    	startActivity(i);
    	
    }
    
    protected void goToItemDisplayPage(){
    	
    	Intent i = new Intent(this, ItemDisplayActivity.class);
    	startActivity(i);
    	
    }
}
